'use strict';
app.factory('VersionService', function(HttpUtilService,$q,APPCONSTANT) {
    /***
     * 获取版本信息
     * @param version
     * @returns {AnimateRunner.promise|*|qFactory.Deferred.promise|td.f.promise|f.promise}
     * {
          "code": "000000",
          "message": "获取版本信息成功",
          "data": {
            "app_type": "IOS",
            "version": "1.1.0",
            "version_count":2,
            "app_url": "http:baidu.com/app.plist",
            "description": "1、增加银联支付 /r/u2、增加秒杀活动"
          },
          "status": true
        }
     */
    function getVersionInfo(versionType){
      var deferred = $q.defer();
      HttpUtilService.httpGetNoToken("Version/Get",{version:versionType})
        .then(function (response) {
        // {
        //  "code": "000000",
        //  "message": "获取版本信息成功",
        //  "data": {
        //    "app_type": "IOS",
        //    "version": "1.1.0",
        //    "version_count":2,
        //    "app_url": "http:baidu.com/app.plist",
        //    "description": "1、增加银联支付 /r/u2、增加秒杀活动"
        //  },
        //  "status": true
        //}
          deferred.resolve(response.data)
      },function (error) {
        console.log("getVersionInfo error:"+ error)
      })
      return deferred.promise;
    }
    function getProvisionInfo(version){
      var deferred = $q.defer();
      HttpUtilService.httpGetNoToken("Provision/Get",{version:version})
      .success(function (response) {
        // {
        //  "Provision_URL": "条款内容地址，Html5的文本内容"
        //}
        if(response){
          deferred.resolve(response.Provision_URL)
          //todo:这里还要确认一下数据格式
        } else {
          console.log("getProvisionInfo:"+ response)
        }
      }).error(function (error) {
        console.log("getProvisionInfo error:"+ error)
      })
      return deferred.promise;
    }
    function getAssureInfo(version){
      var deferred = $q.defer();
      var url = APPCONSTANT.pathUrl + "Provision/Get";
      HttpUtilService.httpGetNoToken("Provision/Get",{version:version})
        .success(function (response) {
        // {
        //  "Assure_URL": "担保协议内容地址，Html5的文本内容"
        //}
        if(response){
          deferred.resolve(response.Assure_URL)
          //todo:这里还要确认一下数据格式
        } else {
          console.log("getAssureInfo:"+ response)
        }
      }).error(function (error) {
        console.log("getAssureInfo error:"+ error)
      })
      return deferred.promise;
    }
    return {
      getVersionInfo:getVersionInfo,
      getProvisionInfo:getProvisionInfo,
      getAssureInfo:getAssureInfo
    };
  })

